﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YunRuiFarm.Models;
using static YunRuiFarm.Models.DatabaseModels;

namespace YunRuiFarm.Controllers
{
    public class YunRuiFarmController : Controller
    {


        // GET: YunRuiFarm
        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Register()
        {
            return View();
        }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Adopt()
        {
            return View();
        }
        public ActionResult AdoptGoat()
        {
            return View();
        }
        public ActionResult GoatAppointment()
        {
            if (Verification())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult UserInfo()
        {
            if (Verification())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        
        public ActionResult Agricultural()
        {
            return View();
        }
        public ActionResult AgriculturalDetail()
        {
            return View();
        }
        public ActionResult Restaurant()
        {
            return View();
        }
        public ActionResult PersonalCenter()
        {
            return View();
        }
        public ActionResult UserGoatOrder()
        {
            if (Verification())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult GoatOrderDetail()
        {
            if (Verification())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult UserAgriculturalOrder()
        {
            if (Verification())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult UserRestaurantOrder()
        {
            if (Verification())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult UserFeedback()
        {
            if (Verification())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsGoatOrder()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsGoatOrderDetail()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsRestaurantOrder()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsAgriculturalOrder()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsFeedback()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsPictureList()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsPictureDetail()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsAgriculturalList()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public ActionResult AdministratorsAgriculturalDetail()
        {
            if (Power())
                return View();
            else
                return Redirect("/YunRuiFarm/Login");
        }
        public bool Verification()
        {
            
            String Token = Request.Cookies["token"]?.Value;
            if (Token == null||Token=="")
            {
                return false;
            }
            else 
            {
                var result = user_token.Find(p => p.Token == Token);
                if (result.Count == 0)
                {
                    return false;
                }
                else
                {
                    if (result[0].Expirytime < DateTime.Now)
                        return false;
                    else
                        return true;
                }
            }
        }
        public bool Power()
        {
            if (Verification())
            {
                String Token = Request.Cookies["token"]?.Value;
                var PersonId = user_token.FindBySql("select * from user_token where Token='"+Token + "'")[0].PersonId;
                var power = user_info.FindById(PersonId).Power;
                if (power == 2)
                    return true;
                else
                    return false;
            }
            else
                return false;
            
        }
    }
}